/*
 * Copyright 2014 alexpad@gmail.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package alexpad.karaokarc.gui.nameChooser;

import alexpad.util.ArtistNameUtil;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.ButtonGroup;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;

/**
 *
 * @author alexpad@gmail.com
 */
public class NameChooserDialog extends javax.swing.JDialog implements ActionListener {
	
	public static final int NONE_SELECTED = -1;
	
	private NameChooserResult result;
	
	private NameOptionPanel firstNameOptionPanel;
	private NameOptionPanel secondNameOptionPanel;
	
	protected NameChooserDialog(java.awt.Frame parent, String fileName) {
		super(parent, fileName, true);
		initComponents();
		
		String artist = null;
		String title;
		fileName = FilenameUtils.getBaseName(fileName);
		int pos = fileName.lastIndexOf('-');
		if(pos>0){
			artist = fileName.substring(0, pos).trim();
			title =  fileName.substring(pos + 1).trim().toLowerCase();
		}
		else{
			title =  fileName;
		}
		
		ButtonGroup titleBtnGroup = new ButtonGroup();
		NameOptionPanel nameOptionPanel;
		if(StringUtils.isBlank(artist)){
			titleOptionsPanel.setLayout(new GridLayout(1, 1));
			nameOptionPanel = new NameOptionPanel(title, true, titleBtnGroup);
			titleOptionsPanel.add(nameOptionPanel);
			
			ButtonGroup artistBtnGroup = new ButtonGroup();
			artistOptionsPanel.setLayout(new GridLayout(1, 1));
			nameOptionPanel = new NameOptionPanel(artist, true, artistBtnGroup);
			artistOptionsPanel.add(nameOptionPanel);
			pack();
		}
		else{
			titleOptionsPanel.setLayout(new GridLayout(2, 1));
			
			firstNameOptionPanel = new NameOptionPanel(artist, false, titleBtnGroup);
			firstNameOptionPanel.addOptionSelectionListener(this);
			firstNameOptionPanel.addOptionTextChangeListener(this);
			titleOptionsPanel.add(firstNameOptionPanel);
			
			secondNameOptionPanel = new NameOptionPanel(title, true, titleBtnGroup);
			secondNameOptionPanel.addOptionSelectionListener(this);
			secondNameOptionPanel.addOptionTextChangeListener(this);
			titleOptionsPanel.add(secondNameOptionPanel);
			
			artistChanged(artist);
		}
		setLocationRelativeTo(getParent());
	}
	
	public static NameChooserResult showDialog(java.awt.Frame parent, String fileName){
		NameChooserDialog dialog = new NameChooserDialog(parent, fileName);
		dialog.setVisible(true);
		return dialog.getNameChooseResult();
	}
	
	protected NameChooserResult getNameChooseResult(){
		return result;
	}
	
	protected void artistChanged(String artist){
		artistOptionsPanel.removeAll();
		
		String[] artistOptions = ArtistNameUtil.getCombinations(artist);
		artistOptionsPanel.setLayout(new GridLayout(artistOptions.length, 1));
		
		ButtonGroup artistBtnGroup = new ButtonGroup();
		for(int n=0;n<artistOptions.length;n++){
			artistOptionsPanel.add(new NameOptionPanel(artistOptions[n], n==0, artistBtnGroup));
		}
		pack();
	}
	
	private void skip(){
		result = new NameChooserResult(NameChooserResult.Action.SKIPPED) ;
		dispose();
	}
	
	private void stop(){
		result = new NameChooserResult(NameChooserResult.Action.STOPPED) ;
		dispose();
	}
	
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        artistLabel = new javax.swing.JLabel();
        titleOptionsPanel = new javax.swing.JPanel();
        confirmBtn = new javax.swing.JButton();
        skipBtn = new javax.swing.JButton();
        stopBtn = new javax.swing.JButton();
        titleLabel = new javax.swing.JLabel();
        artistOptionsPanel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        artistLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        artistLabel.setText("Artista");
        artistLabel.setToolTipText("");

        titleOptionsPanel.setLayout(null);

        confirmBtn.setText("Confirm");
        confirmBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                confirmBtnActionPerformed(evt);
            }
        });

        skipBtn.setText("Skip");
        skipBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                skipBtnActionPerformed(evt);
            }
        });

        stopBtn.setText("Stop");
        stopBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stopBtnActionPerformed(evt);
            }
        });

        titleLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        titleLabel.setText("Titolo");

        artistOptionsPanel.setLayout(null);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(confirmBtn)
                        .addGap(120, 120, 120)
                        .addComponent(skipBtn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 370, Short.MAX_VALUE)
                        .addComponent(stopBtn))
                    .addComponent(artistLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(titleOptionsPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(titleLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(artistOptionsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addComponent(titleLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(titleOptionsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 17, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(artistLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(artistOptionsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 17, Short.MAX_VALUE)
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(confirmBtn)
                    .addComponent(skipBtn)
                    .addComponent(stopBtn))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void confirmBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_confirmBtnActionPerformed
		String artist = null;
		String title = null;
		NameOptionPanel nameOptionPanel;
		for(Component c : artistOptionsPanel.getComponents()){
			nameOptionPanel = (NameOptionPanel) c;
			if(nameOptionPanel.isSelected()){
				artist = nameOptionPanel.getText();
				break;
			}
		}
		for(Component c : titleOptionsPanel.getComponents()){
			nameOptionPanel = (NameOptionPanel) c;
			if(nameOptionPanel.isSelected()){
				title = nameOptionPanel.getText();
				break;
			}
		}
		result = new NameChooserResult(artist, title);
		dispose();
    }//GEN-LAST:event_confirmBtnActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        stop();
    }//GEN-LAST:event_formWindowClosing

    private void skipBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_skipBtnActionPerformed
        skip();
    }//GEN-LAST:event_skipBtnActionPerformed

    private void stopBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopBtnActionPerformed
        stop();
    }//GEN-LAST:event_stopBtnActionPerformed
	
	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if(firstNameOptionPanel.isOptionSelector(source)){
			artistChanged(secondNameOptionPanel.getText());
		}
		else if(secondNameOptionPanel.isOptionSelector(source)){
			artistChanged(firstNameOptionPanel.getText());
		}
		else if(firstNameOptionPanel.isOptionText(source)){
			if(!firstNameOptionPanel.isSelected()){
				artistChanged(firstNameOptionPanel.getText());
			}
		}
		else if(secondNameOptionPanel.isOptionText(source)){
			if(!secondNameOptionPanel.isSelected()){
				artistChanged(secondNameOptionPanel.getText());
			}
		}
	}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel artistLabel;
    private javax.swing.JPanel artistOptionsPanel;
    private javax.swing.JButton confirmBtn;
    private javax.swing.JButton skipBtn;
    private javax.swing.JButton stopBtn;
    private javax.swing.JLabel titleLabel;
    private javax.swing.JPanel titleOptionsPanel;
    // End of variables declaration//GEN-END:variables

}
